﻿using System;
using DevelopersTestPad.Worksheets;

namespace DevelopersTestPad.AppFrame
{
    /// <summary>
    /// Defines which app command are currently available and which are not.
    /// </summary>
    internal sealed class AppCommandAvailabilities
    {
        private Worksheet CurrentWorksheet { set; get; }

        /// <summary>
        /// No public constructor available.
        /// </summary>
        private AppCommandAvailabilities()
        {            
        }

        /// <summary>
        /// Creates a new AppCommandAvailabilities instance.
        /// </summary>
        /// <param name="currentWorksheet">The current worksheet or null on not available.</param>
        /// <returns>The object that defines which app command are currently available and which are not.</returns>
        internal static AppCommandAvailabilities Analyze(Worksheet currentWorksheet)
        {
            return new AppCommandAvailabilities {CurrentWorksheet = currentWorksheet};
        }

        /// <summary>
        /// Checks if the specified app command is currently available.
        /// </summary>
        /// <param name="command">The app command to check.</param>
        /// <returns>true/false</returns>
        internal bool IsAvailable(AppCommandType command)
        {
            bool available = false;

            switch (command)
            {
                case AppCommandType.NewEditorPage:
                    available = true;
                    break;

                case AppCommandType.OpenFile:
                    available = true;
                    break;

                case AppCommandType.SaveFile:
                    if (CurrentWorksheet != null && (CurrentWorksheet.Status == WorksheetStatus.Initialized || CurrentWorksheet.Status == WorksheetStatus.Dirty))
                    {
                        available = true;
                    }
                    break;

                case AppCommandType.SaveFileAs:
                    if (CurrentWorksheet != null)
                    {
                        available = true;
                    }
                    break;

                case AppCommandType.ExitApplication:
                    available = true;
                    break;

                case AppCommandType.Execute:
                    if (CurrentWorksheet != null && CurrentWorksheet.Status != WorksheetStatus.Initialized)
                    {
                        available = true;
                    }
                    break;

                case AppCommandType.ShowOptions:
                    available = true;
                    break;

                case AppCommandType.ShowAbout:
                    available = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("command");
            }

            return available;
        }
    }
}
